#pragma once 
#include<vector>
#include"picture.h"
#include<graphics.h>
#include"bullet.h"
#include"player.h"
 int score=0;
using namespace std;
static Atlas* enemyleft;
static Atlas* enemyright;

class Enemy
{
public:
	int health = 20;
private:
	
	int hurt = 1;
	const int Enemy_SPEED = 2;
	const int Enemy_height =80;
	const int Enemy_width = 80;
	const int shadow = 32;
	POINT Enemy_pos = { 0,0 };
	bool move_left = 0;
	Picture* Enemy_left;
	Picture* Enemy_right;
	IMAGE Enemy_shadow;
	bool alive = 1;
	int aidatime = 50;
public:
	void hurtenemy(const Bullet& bullet)
	{
		bool killx = Enemy_pos.x <= bullet.position.x && Enemy_pos.x + Enemy_width >= bullet.position.x;
		bool killy = Enemy_pos.y <= bullet.position.y && Enemy_pos.y + Enemy_height >= bullet.position.y;
		if( killx && killy)
         this->health-= playerhurt;
	}
	bool judge(const Bullet& bullet)
	{
		return (Enemy_pos.x <= bullet.position.x && Enemy_pos.x + Enemy_width >= bullet.position.x) &&
			(Enemy_pos.y <= bullet.position.y && Enemy_pos.y + Enemy_height >= bullet.position.y);
	}

	bool killplayer(const Player& player)
	{
		POINT position = { Enemy_pos.x + Enemy_width / 2,Enemy_pos.y + Enemy_height / 2 };
		bool killx = player.getplayer().x <= position.x && player.getplayer().x + player_width >= position.x;
		bool killy = player.getplayer().y <= position.y && player.getplayer().y + player_height >= position.y;
		if (killx && killy)
		{
			playerhealth = playerhealth - hurt;
			alive = false;
			score--;
		 }
		if (playerhealth <= 0)
		{
			return 1;
		}
		return 0;
	}

	Enemy()
	{
		loadimage(&Enemy_shadow, _T("D:/ConsoleApplication1/ConsoleApplication1/back/shadow_enemy.png"));
		Enemy_left = new Picture(enemyleft, 6);
		Enemy_right = new Picture(enemyright, 6);
		enum class  Edge
		{
			UP = 0,
			DOWN,
			LEFT,
			RIGHT
		};
		Edge edge = (Edge)(rand() % 4);
		switch (edge)
		{
		case Edge::UP:
		{
			Enemy_pos.x = rand() % 1280;
			Enemy_pos.y = 0;
			break;
		}
		case Edge::DOWN:
		{
			Enemy_pos.x = rand() % 1280;
			Enemy_pos.y = 960;
			break;
		}
		case Edge::LEFT:
		{
			Enemy_pos.y = rand() % 960;
			Enemy_pos.x = 1280;
			break;
		}
		case Edge::RIGHT:
		{
			Enemy_pos.y = rand() % 960;
			Enemy_pos.x = 0;
			break;
		}
		}
	}
	~Enemy()
	{
		delete Enemy_left;
		delete Enemy_right;
	}
	void DrawEnemy(int delta, const Player& player)
	{
		int shadow_pos_x = Enemy_pos.x + (Enemy_width - shadow) / 2;
		int shadow_pos_y = Enemy_pos.y + shadow - 8;
		putimage_a(shadow_pos_x, shadow_pos_y, &Enemy_shadow);
		POINT pos = player.getplayer();

		int idx_x = pos.x - Enemy_pos.x;
		if (idx_x > 0)
		{
			Enemy_right->Play(Enemy_pos.x, Enemy_pos.y, delta);
		}
		else
		{
			Enemy_left->Play(Enemy_pos.x, Enemy_pos.y, delta);
		}
		static TCHAR text[64];
		_stprintf_s(text, _T("%d"), health);
		setbkmode(TRANSPARENT);
		settextcolor(RGB(255, 85, 185));
		outtextxy(Enemy_pos.x+40, Enemy_pos.y , text);
	}
	void move(const Player& player)
	{
		const POINT& position = player.getplayer();
		int x = position.x - Enemy_pos.x;
		int y = position.y - Enemy_pos.y;
		double length = sqrt(x * x + y * y);
		if (length != 0)
		{
			int move_x = (int)(Enemy_SPEED * x / length);
			int move_y = (int)(Enemy_SPEED * y / length);
			Enemy_pos.x += move_x;
			Enemy_pos.y += move_y;
		}
	}//////////////////////////////////////////////////////////////////////////////////
	void hunt()
	{
		if (health <= 0)
		alive = 0;
	}
	bool checklive()
	{
		return alive;
	}
	void aidasuan()
	{
		aidatime++;
	}
	void zeroaida()
	{
		aidatime = 0;
	}
    bool canaida()
	{
		//const int aitime = 1250;
		if (aidatime > 20)
		{
			return 1;
		}
		else
			return 0;
	}

};
static void createnewenemy(vector<Enemy*>& Enemys,int score)
{
	
	const int everytime = 200-score;
	static int time = 0;

	if (time++ % everytime == 0)
	{
		Enemys.push_back(new Enemy());

	}
}